$("header").load("header.html");
$("footer").load("footer.html");



// 渲染购物测商品
function handleShopp() {
    let allShopp = document.querySelector(".all-shopp-goods");
    let locsl = localStorage;
    let shoppGoodsSum = JSON.parse(locsl.shoppGoodsSum);
    let sumUp = 0;
    for (let i in shoppGoodsSum) {
        sumUp++;
    }
    for (let i = 0; i < sumUp / 5; i++) {
        let goodsmMoney = JSON.parse(locsl.goods);
        goodsmMoney = goodsmMoney[i + "money"];
        allShopp.innerHTML += `<tr class="cart-table-row cart-table-row__active">
            <td width="38" class="cart-table__center">
                <div class="pc-lining-ckb pc-lining-ckb__checked"><input type="checkbox"><label></label></div>
            </td>
            <td width="100">
                <div class="cart-table-thumb">
                    <div class="lazyload-wrapper" style="font-size: 0px;"><img src="imgs-goods/${shoppGoodsSum["a"+i]}0.jpg" class="pc-lining-image " alt=""></div>
                </div>
            </td>
            <td width="240"><span class="cart-table-name">${shoppGoodsSum["d"+i]}</span></td>
            <td width="100"><span class=""><div>${shoppGoodsSum["b"+i]}</div><div>${shoppGoodsSum["c"+i]}</div></span></td>
            <td width="100">
                <div class="goods-price"><span class="price-tag-label">直降价：</span>
                    <div class="price wr-className price-class">
                        <div class="symbol symbol-className">￥</div>
                        <div class="pprice">
                            <div class="integer goods-money inline">${goodsmMoney}</div>
                            <div class="decimal inline decimal-className">.00</div>
                        </div>
                    </div>
                </div>
            </td>
            <td width="150">
                <div class="stepper"  id="${shoppGoodsSum["a"+i]}">
                    <div class="stepper__minus stepper__minus__disabled">
                    <span class="stepper__minus__icon">-</span></div>
                    <input class="stepper__input" value="${shoppGoodsSum["e"+i]}">
                    <div class="stepper__plus"><span class="stepper__plus__icon">+</span></div>
                </div>
            </td>
            <td width="100"><span id="${shoppGoodsSum["a"+i]}" class="cart-link">删除</span></td>
        </tr>`;
    }



}


// 总价
function totalPrices() {
    // $(".goods-money")
    // console.log($(".goods-money").length);
    // let ss = $(".goods-money").eq(i).parse.parse.parse.parse.next().find(".stepper__input");
    // console.log(ss)
    let sum = 0;
    for (let i = 0; i < $(".goods-money").length; i++) {
        sum += +$(".goods-money").eq(i).html() * +$(".stepper__input").eq(i).val();
    }
    $(".gooods-total-prices").html(sum);
}



// 数据库商品数量加减
function changeGoodsSum(sum,id) {
    let locsl = localStorage;
    console.log("clientName="+locsl.name+"&goodsId="+id+"&goodsSum="+ +sum);
    $.ajax({
        type:"GET",
        url:"php/changeGoodsSum.php",
        data:"clientName="+locsl.name+"&goodsId="+id+"&goodsSum="+ +sum
        // data:"clientName="+locsl.name+"&goodsId="+id+"&goodsSum="+ +sum,
    })


}


function goodsSull(locsl) {

    // 数量++
    $(".stepper__minus").click(function() {
        if ($(this).next().val() > 1) {
            $(this).next().val(+$(this).next().val() - 1);
            totalPrices();
            // 更新数据库
            changeGoodsSum($(this).next().val(),$(this).parent().attr("id"));
        }
    })
    // 数量--
    $(".stepper__plus").click(function() {
        $(this).prev().val($(this).prev().val() - -1);
        totalPrices();
        // 更新数据库
        changeGoodsSum($(this).prev().val(),$(this).parent().attr("id"));
    })

    // 点击删除发送数据到后端
    $(".cart-link").click(function() {
        $(this).parent().parent().remove();
        totalPrices();
        // console.log(locsl.name)

        $.ajax({
            type:"GET",
            url:"php/delGoods.php",
            data:"clientName="+locsl.name+"&goodsId="+$(this).attr("id"),
            success: function(msg) {
                shoppgood(locsl);
            }
        })
    })

}

// 删除后得回调函数
// 删除后获取重新获取数据加载到 localStorage 
function shoppgood(locsl){
    $.ajax({
        type: "GET",
        url: "php/toGoods.php",
        data: "logId=" + locsl.name,
        success: function(msg) {
            locsl.shoppGoodsSum = msg;
            // 刷新头部购物车数量
            let allShoppGoods = JSON.parse(locsl.shoppGoodsSum);
            let clientShoppGoodsNum = 0;
                // 遍历json长度赋值给购物车数量
            for (let index in allShoppGoods) {
                // console.log(allShoppGoods[index]);
                clientShoppGoodsNum++;
            }

            // 重新加载头部购物车数量
            if(clientShoppGoodsNum == 0){
                $(".header-shopp-sum").css("display","none")
            }else{
                $(".header-shopp-sum").html(clientShoppGoodsNum / 5);
            }
            
        }
    })
}

$(document).ready(function() {
    let locsl = localStorage;
    if (locsl.shoppGoodsSum != 0 && locsl.shoppGoodsSum != undefined) {
        $(".bgm").css("display", "none");
        handleShopp();
        totalPrices();
        goodsSull(locsl);

    } else {
        $(".bgm").css("display", "block");
        $(".cart").css("display", "none");
    }
    $(".header-quit").click(function () {
        $(location).attr('href', 'index.html');
    })
})
$(".header-quit").click(function() {

    $(".all-shopp-goods").html(" ");
    totalPrices();
})
